function generateRoutes(userPermissionsTree) {

    let flatPermissions = [];
    flatPermissionTreeToArray(userPermissionsTree, flatPermissions);

    let components = flatPermissions.filter(permission => {
        return permission.permissionType == 'C';
    });

    let routes = components.map(permission => {
        let route = {
            path: permission.path,
            name: permission.permissionName,
            component: () => import(`@/views/${permission.component}.vue`)
        }
        if (permission.children) {
            route.children = generateRoutes(permission.children);
        }
        return route;
    })

    return routes;
}


function flatPermissionTreeToArray(userPermissionsTree, flatPermissions) {
    for (let i = 0; i < userPermissionsTree.length; i++) {
        flatPermissions.push(userPermissionsTree[i]);
        if (userPermissionsTree[i].children) {
            flatPermissionTreeToArray(userPermissionsTree[i].children, flatPermissions);
        }
    }
}

export { generateRoutes, flatPermissionTreeToArray }